package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgSectionCommodityRelationBO;
import com.ztgf.shopping.models.bo.epg.EpgSectionCommodityRelationSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgSectionCommodityRelationDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgSectionCommodityRelationService;
import com.ztgf.shopping.models.vo.epg.EpgSectionCommodityRelationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 频道栏目控制器
 *
 * @author YUE
 * @create 2020-05-18 11:36
 */

@RestController
@RequestMapping("/cms/epgSectionCommodityRelation")
@Slf4j
@Api(value = "EpgSectionCommodityRelationController",tags = "频道栏目商品表")
public class EpgSectionCommodityRelationController {

    @DubboReference
    private IEpgSectionCommodityRelationService iEpgSectionCommodityRelationService;

    @ApiOperation(value = "频道栏目商品表根据频道id",notes = "频道栏目商品表根据频道id")
    @PostMapping("/getListBySectionId")
    public Result<List<EpgSectionCommodityRelationVO>> getListBySectionId(Long sectionId){
        EpgSectionCommodityRelationBO epgSectionCommodityRelationBO = new EpgSectionCommodityRelationBO();
        epgSectionCommodityRelationBO.setSectionId(sectionId);
        List<EpgSectionCommodityRelationVO> sectionCommodityRelationVOList = iEpgSectionCommodityRelationService.getListBySectionId(sectionId);
        return new Result<>(sectionCommodityRelationVOList);
    }

    @ApiOperation(value = "添加频道栏目商品表",notes = "添加频道栏目商品表")
    @PostMapping(value = "/insert")
    public Result insert( EpgSectionCommodityRelationSaveBO epgSectionCommodityRelationSaveBO) {
        if(null == epgSectionCommodityRelationSaveBO.getSort())//没有排序编号
            return new Result<>(CodeEnum.FAIL_100033);
        boolean flag = iEpgSectionCommodityRelationService.add(epgSectionCommodityRelationSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }


    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EpgSectionCommodityRelationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        EpgSectionCommodityRelationDTO epgSectionCommodityRelationDTO = iEpgSectionCommodityRelationService.selectById(id);
        return new Result<>(epgSectionCommodityRelationDTO);
    }


    @PostMapping("/save")
    @ApiOperation(value = "保存频道栏目商品表",notes = "保存频道栏目商品表")
    public Result save( EpgSectionCommodityRelationSaveBO epgSectionCommodityRelationSaveBO){
        if(epgSectionCommodityRelationSaveBO.getId() == null){
            //添加
            return  insert(epgSectionCommodityRelationSaveBO);
        } else {
            return  update(epgSectionCommodityRelationSaveBO);
        }
    }

    @ApiOperation(value = "修改频道栏目商品表",notes = "修改频道栏目商品表")
    @PostMapping("/update")
    public Result update( EpgSectionCommodityRelationSaveBO epgSectionCommodityRelationSaveBO){
        EpgSectionCommodityRelationDTO epgSectionCommodityRelationDTO = new EpgSectionCommodityRelationDTO();
        BeanUtils.copyProperties(epgSectionCommodityRelationSaveBO, epgSectionCommodityRelationDTO);
        epgSectionCommodityRelationDTO.setId(epgSectionCommodityRelationSaveBO.getId());
        boolean flag = iEpgSectionCommodityRelationService.updateById(epgSectionCommodityRelationDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除频道栏目商品表",notes = "删除频道栏目商品表")
    @ApiImplicitParam(name = "id",value = "字典表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = iEpgSectionCommodityRelationService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除频道栏目商品表",notes = "批量删除频道栏目商品表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        iEpgSectionCommodityRelationService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @ApiOperation(value = "频道栏目商品表列表",notes = "频道栏目商品表列表")
    @PostMapping("/queryPageList")
    public Result<Page<EpgSectionCommodityRelationDTO>> queryPageList( EpgSectionCommodityRelationBO epgSectionCommodityRelationBO){
        Wrapper<EpgSectionCommodityRelationDTO> wrapper = new EntityWrapper<>();
        Page<EpgSectionCommodityRelationDTO> page = new Page<>(epgSectionCommodityRelationBO.getPageNo(), epgSectionCommodityRelationBO.getPageSize());
        page = iEpgSectionCommodityRelationService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @ApiOperation(value = "频道栏目商品表排序",notes = "频道栏目商品表排序")
    @PostMapping("/queryPageOrderList")
    public Result<Page<EpgSectionCommodityRelationDTO>> queryPageBySortList( EpgSectionCommodityRelationBO epgSectionCommodityRelationBO){
        Wrapper<EpgSectionCommodityRelationDTO> wrapper = new EntityWrapper<>();
        Result result = new Result();
        if(null == epgSectionCommodityRelationBO.getSort()){
            result.setCode(CodeEnum.FAIL.getCode());
            result.setData("传入排序规则为空");
            return result;
        }
        if(epgSectionCommodityRelationBO.getSort().equals(ShoppingCodeEnum.SORT_ASC.getCode()))//降序
            wrapper.orderBy("sort", true);
        else
            wrapper.orderBy("sort", false);
        Page<EpgSectionCommodityRelationDTO> page = new Page<>(epgSectionCommodityRelationBO.getPageNo(), epgSectionCommodityRelationBO.getPageSize());
        page = iEpgSectionCommodityRelationService.selectPage(page);
        return new Result<>(page);
    }
}
